{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "18a55a4a",
   "metadata": {},
   "outputs": [],
   "source": [
    "from gs_quant.session import Environment, GsSession\n",
    "from gs_quant.markets.index import Index"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "## Pre Requisites\n",
    "To use below functionality on **STS Indices**, your application needs to have access to the following datasets:\n",
    "1. [STS_UNDERLIER_WEGHTS](https://marquee.gs.com/s/developer/datasets/STS_UNDERLIER_WEGHTS) - Weights of index underliers of STS Indices\n",
    "5. [STS_UNDERLIER_WEGHTS](https://marquee.gs.com/s/developer/datasets/STS_UNDERLIER_WEGHTS) - Attribution of index underliers\n",
    "\n",
    "You can request access by going to the Dataset Catalog Page linked above.\n",
    "\n",
    "Note - Please skip this if you are an internal user"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "14915132",
   "metadata": {},
   "outputs": [],
   "source": [
    "# external users should substitute their client id and secret; please skip this step if using internal jupyterhub\n",
    "GsSession.use(Environment.PROD, client_id=None, client_secret=None, scopes=('read_product_data',))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "315d7921",
   "metadata": {},
   "outputs": [],
   "source": [
    "strategy_index = Index.get('GSXXXXXX') # substitute input with any identifier for an index"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "20b48849",
   "metadata": {},
   "source": [
    "### These functions currently supports STS indices only"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5e5df9ff",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Returns the root node of the underlier tree formed by the index \n",
    "strategy_index.get_underlier_tree()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9dcdf9da",
   "metadata": {},
   "source": [
    "To know more about how to use the AssetTreeNode object, please refer to the example notebook for the Tree Entity class, found in gs_quant/documentation/08_tree_entity"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "15dfe760",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Returns the full tree formed by the the index as a pandas dataframe\n",
    "strategy_index.get_underlier_tree().to_frame()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# This functionality requires treelib to be installed\n",
    "# Prints the tree structure formed by the Index for easy visualisation\n",
    "strategy_index.visualise_tree(visualise_by='name')\n",
    "\n",
    "# If data is missing for any field, then assetId will be used instead\n",
    "strategy_index.visualise_tree(visualise_by='bbid')\n",
    "\n",
    "strategy_index.visualise_tree(visualise_by='id')\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "900625d8",
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "# To refresh and rebuild the tree\n",
    "strategy_index.get_underlier_tree(refresh_tree=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "*Have any other questions? Reach out to the [Marquee STS team](mailto:gs-marquee-sts-support@gs.com)!*"
   ],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}